\documentclass[12pt]{article}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{times}
\usepackage{verbatim}

\newtheorem{defn}{Definition}
\newtheorem{lem}{Lemma}
\newtheorem{thm}{Theorem}

\begin{document}
\section*{Administrivia}
\begin{itemize}

\end{itemize}

\section*{Kernel Assignment}
\paragraph{}
Starting from the next assignment, we will use the \verb=-r= flag to
RedBoot's \verb=load=. This will \textit{not} strip the symbol table from
your ELF files, as RedBoot is doing now. There are a number of hard tasks:
\begin{itemize}
\item Getting the parts of the ELF file that you need. You will need to
inspect the ELF file and find various function entry points.
\item Context switching. We will discuss this today.
\item Scheduling. (This is actually easy, for Real-Time values of easy; you
are given the scheduling algorithm, and must implement it in constant time.)
\end{itemize}

\paragraph{}
Previously the context switch would take up to 100 lines of assembly code.
RedBoot should leave the box in a comparatively clean state; the address space
is flat, and a large chunk of memory above \verb=0x218000= is available for
your use. In addition, memory management and caching are disabled by
default. This last detail is important: disabling the cache makes context
switching easier, but it causes your program to run several orders of
magnitude slower.

\paragraph{}
Upon kernel initialization, the kernel will build a number of data structures -
task descriptor tables, memory management tables, pointers to device drivers,
and so on. You will recall that the basic kernel operation looks like this:
\begin{verbatim}
init();
FOREVER {
  schedule();
  nxtReq = activate();
  handle(nxtReq);
}
\end{verbatim}

\paragraph{}
The software interrupt instruction \verb=swi <n>= does 
\( lr \leftarrow pc + 4 \); \(pc \leftarrow 0x00000008 \);
\( spsw \leftarrow cpsw \). Once you
put something into address \verb=0x00000008= (e.g. \verb=b entry=), it
will be executed upon issuing \verb=swi=. (There is in fact a bunch of stuff
down near address 8 in memory - when we do real-time debugging, we'll
see the \verb=brkp= command.) The status words \verb=spsw=, \verb=cpsw= are 
used for a variety of things; in particular, the low-order bits can be used
to manipulate interrupts. These words cannot be reached from ``user mode'' -
it \textit{must} be accessed via \verb=swi=.

\paragraph{}
We mentioned user mode in the previous paragraph. In user mode, the ARM has
16 registers, a stack pointer, a link register, a program counter, and a 
current status word. SVC mode has registers, a \textit{separate} stack pointer,
a \textit{separate} current status word, and an additional saved status word.
In essence: the kernel has its own private stack pointer. This makes it hard
to access the user mode stack pointer, which in turn makes it hard to save
user mode state. To deal with this, the ARM provides a third intermediate
state, or system state. System state has all the privilege of SVC mode, but
it shares the user mode stack pointer. Hence, to save user state:

\begin{itemize}
\item \verb=swi= into kernel in SVC (service) mode. (The mode part is handled
automatically.)
\item Grab task arguments, which are stored in the first few registers.
(Does this go here?)
\item Restore kernel state. (Does this go here?)
\item Switch to system mode, grab the user state, and store the user stack
pointer into the descriptor table.
\item Switch back to service mode and continue with scheduling, task
activation/handling, etc.
\end{itemize}

\paragraph{}
You will recall that \verb=swi= takes an argument, which tells it where to
jump to. To compute that value, you can perform, e.g.
\( r0 \leftarrow lr - 4\).

\paragraph{}
As an exercise, you may want to write a program that does nothing except
call an empty function. Inspect the assembly code and you will find:
\begin{itemize}
\item Setting up the stack frame. These instructions manipulate the
instruction, frame, and stack pointers.
\item Saving the current state on the stack. This is usually performed by
issuing a \verb=stmfd sp!, {r0-r12,lr}= or something like that.
\item Jumping to the function. The link register and program counter are 
manipulated using something like \verb=bl=.
\end{itemize}
\paragraph{}
These instructions have counterpoints for returning from the function. For
instance, \verb=ldmia sp!, {r0-r12,pc}= undoes the store instructions
previously performed by \verb=stmfd=.

\paragraph{} 
Tasks \textit{must} be re-entrant, i.e. it must have no static or global
state except for constants. They should also be position-independent. This
is because several copies of the same task may run concurrently; for instance,
a task might control the speed for one train, or the operation of one switch.

\end{document}
